package cn.edu.xcu.ckl;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.lzy.okgo.OkGo;

import cn.edu.xcu.ckl.api.ApiConstants;
import cn.edu.xcu.ckl.base.BaseActivity;
import cn.edu.xcu.ckl.http.HttpStringCallback;

public class RegisterActivity extends BaseActivity {

    private EditText et_username;
    private EditText et_password;
    private RadioGroup radio;

    private String gender = "男";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        //初始化控件
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        radio = findViewById(R.id.radio);

    }

    @Override
    protected void setListener() {


        //男女选择
        radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.man) {
                    gender = "男";
                } else {
                    gender = "女";
                }
            }
        });

        //点击注册
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString();
                String password = et_password.getText().toString();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(RegisterActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                } else {
                    register(username, password);
                }

            }
        });
    }

    @Override
    protected void initData() {

    }

    /**
     * 注册
     */
    private void register(String username, String password) {
        OkGo.<String>get(ApiConstants.REGISTER_URL)
                .params("username", username)
                .params("password", password)
                .params("gender", gender)
                .params("register_type", 0)
                .execute(new HttpStringCallback(mBaseActivity) {
                    @Override
                    protected void onSuccess(String msg, String response) {
                        showToast(msg);
                        finish();
                    }

                    @Override
                    protected void onError(String response) {
                        showToast(response);
                    }
                });

    }
}